﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BodyFormat
{
    public class KeyValue
    {
        public KeyValue() { 
        
        }
       private List<String> format = new List<String>();

       public string stringify(params object[] pars)
       {
           String keyval = "";
           if(pars.Length > 0){
               for (int i = 0;i < pars.Length; i++)
               {
                   Dictionary<string, Object> par = (Dictionary<string, Object>)pars[i];
                   keyval += String.Format("{0}={1}&", par.Keys.ToArray()[0], par.Values.ToArray()[0]);
               }
           }
           return keyval.TrimEnd(new char[] { '&'});
        }


       public string build(IEnumerator en)
       {
           buildNode(en);
           string keyval = "var keyval = KeyVal.stringify([" + "\r\n";
           foreach(String fm in format){
               keyval += fm + "\r\n";
           }
           return keyval = keyval.TrimEnd(new char[] { ',' }) + "\r\n" + "])";
       }

        private void buildNode(IEnumerator en)
        {
            while (en.MoveNext())
            {
                //获取转换当前节点数据类型
                DevExpress.XtraTreeList.Nodes.TreeListNode node = (DevExpress.XtraTreeList.Nodes.TreeListNode)en.Current;
                if (node.Nodes.Count > 0)
                {
                    buildNode(node.Nodes.GetEnumerator());
                }
                else {
                    //String ss = @"""" + node.GetValue("Key") + "=${" + node.GetValue("Type") + @"}&"" +";
                    string ss = @"{""" + node.GetValue("Key") + @""":" + @"Bodys.get(""" + node.GetValue("Key") + @""")},";
                    format.Add(ss);
                }
            }
        }

    }
}
